package com.itany.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itany.cloud.constant.TsConstant;
import com.itany.cloud.dao.FlightMapper;
import com.itany.cloud.entity.Flight;
import com.itany.cloud.exception.RequstParameterErrorException;
import com.itany.cloud.service.FlightService;
import com.itany.cloud.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class FlightServiceImpl implements FlightService {

    @Autowired
    private FlightMapper flightMapper;

    @Override
    public void addFlight(String no, String company, String model, String totalSeats, String leaveCity, String arrivalCity, String economyPrice, String firstPrice, String leaveTime, String arrivalTime) throws RequstParameterErrorException {
        if (ParameterUtil.isNull(no)||ParameterUtil.isNull(company)||ParameterUtil.isNull(model) || ParameterUtil.isNull(totalSeats)||ParameterUtil.isNull(leaveCity) || ParameterUtil.isNull(economyPrice) || ParameterUtil.isNull(firstPrice) || ParameterUtil.isNull(leaveTime) || ParameterUtil.isNull(arrivalTime)) {
            throw new RequstParameterErrorException("请求参数有误");
        }
        Flight flight = new Flight(no,company,model,Integer.parseInt(totalSeats),leaveCity,arrivalCity,Double.parseDouble(economyPrice),Double.parseDouble(firstPrice),leaveTime,arrivalTime);
        flightMapper.insert(flight);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Page<Flight> findByParams(String pageNo, String pageSize, String leaveCity, String arrivalCity, String no, String company, String leaveTime, String arrivalTime) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = TsConstant.FLIGHT_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = TsConstant.FLIGHT_DEFAULT_PAGE_SIZE;
        }
        Page<Flight> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<Flight> wrapper = new QueryWrapper<>();
        if(!ParameterUtil.isNull(leaveCity)) {
            wrapper.like("leave_city", leaveCity);
        }
        if(!ParameterUtil.isNull(arrivalCity)) {
            wrapper.like("arrival_city", arrivalCity);
        }
        if(!ParameterUtil.isNull(no)) {
            wrapper.like("no", no);
        }
        if(!ParameterUtil.isNull(company)) {
            wrapper.like("company", company);
        }
        if(!ParameterUtil.isNull(leaveTime)) {
            wrapper.eq("leave_time", leaveTime);
        }
        if(!ParameterUtil.isNull(arrivalTime)) {
            wrapper.eq("arrival_time", arrivalTime);
        }
        return flightMapper.selectPage(page, wrapper);
    }
}
